<?php

namespace App\Http\Controllers\Admin;


use App\Models\Activity;
use App\Models\ActivityUsers;
use App\Models\Admin;
use App\Models\Area;
use App\Models\Article;
use App\Models\AuthMenu;
use App\Models\AuthRole;
use App\Models\AuthRoleMenu;
use App\Models\House;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function getIndexData()
    {
        // 获取昨天的开始时间和结束时间
        $startOfYesterday = Carbon::yesterday()->startOfDay();
        $endOfYesterday = Carbon::yesterday()->endOfDay();

        $result = [];
        $news = Article::where('class_id', 1)->count();
        $activity = Article::where('class_id', 2)->count();
        $comment = Area::count();
        $users = House::count();

        $news_yes = Article::where('class_id', 1)->whereBetween('created_at', [$startOfYesterday, $endOfYesterday])->count();
        $activity_yes = Article::where('class_id', 2)->whereBetween('created_at', [$startOfYesterday, $endOfYesterday])->count();
        $comment_yes = 0;
        $users_yes = House::whereBetween('created_at', [$startOfYesterday, $endOfYesterday])->count();

        $result['comment'] = $comment;
        $result['news'] = $news;
        $result['activity'] = $activity;
        $result['users'] = $users;

        $result['comment_yes'] = $comment_yes;
        $result['news_yes'] = $news_yes;
        $result['activity_yes'] = $activity_yes;
        $result['users_yes'] = $users_yes;

        return showMsg('获取成功', 200, $result);
    }

    //获取用户条形图
    public function getUsersData()
    {
        $dates = get_week_arr(30, time());
        $data = array();
        foreach (json_decode($dates) as $key => $val) {
            $nums = Users::whereDate('created_at', $val)->count();
            $data[$key]['日期'] = $val;
            $data[$key]['数量'] = $nums;
        }
        return showMsg('获取成功', 200, $data);
    }

    public function getNewsData()
    {
        $dates = get_week_arr(30, time());
        $data = array();
        foreach (json_decode($dates) as $key => $val) {
            $nums = Article::whereDate('created_at', $val)->count();
            $data[$key]['日期'] = date('m-d', strtotime($val));
            $data[$key]['数量'] = $nums;
        }
        return showMsg('获取成功', 200, $data);
    }

    public function getActivityData()
    {
        $dates = get_week_arr(30, time());
        $data = array();
        foreach (json_decode($dates) as $key => $val) {
            $nums = Activity::whereDate('created_at', $val)->count();
            $data[$key]['日期'] = date('m-d', strtotime($val));
            $data[$key]['数量'] = $nums;
        }
        return showMsg('获取成功', 200, $data);
    }

    public function getYtkMenus(Request $request)
    {
        $role_id = Admin::where('id', $this->aId)->value('role_id');
        $menuList1 = AuthRoleMenu::leftJoin('auth_menu', function ($join) {
            $join->on('auth_menu.menu_id', '=', 'auth_role_menu.menu_id');
            $join->orderBy('auth_menu.display_order', 'asc');
        })->where('auth_role_menu.role_id', $role_id)->where('auth_menu.status', 1)->select('auth_menu.menu_id', 'auth_menu.name', 'auth_menu.icon', 'auth_menu.menu_id', 'auth_menu.cell', 'auth_menu.url', 'auth_menu.parent_menu_id')->get()->toArray();

        $menuArr1 = array();
        foreach ($menuList1 as $v) {
            $v["check"] = 0;
            $menuArr1[$v["menu_id"]] = $v;
        }
        //角色当前拥有的菜单
        $menuList2 = AuthRoleMenu::where("role_id", $role_id)->get();

        foreach ($menuList2 as $v) {
            $menu_id = $v["menu_id"];
            if (isset($menuArr1[$menu_id])) {
                $menuArr1[$menu_id]["check"] = 1;
            }
        }

        //遍历菜单生成唯一主键的数组
        $menuArr2 = array();
        foreach ($menuArr1 as $v) {
            $menu_id = $v["menu_id"];
            if ($v["cell"] == "1") {
                $menuArr2[$menu_id] = $v;
            } elseif ($v["cell"] == "2") {
                $parent_menu_id = $v["parent_menu_id"];
                if (empty($menuArr2[$parent_menu_id]['role_id'])) {
                    $menu = AuthMenu::where('menu_id', $v['parent_menu_id'])->first();
                    if ($menu['status'] == 1) {
                        $menuArr2[$parent_menu_id]['link'] = $menu['url'];
                        $menuArr2[$parent_menu_id]['name'] = $menu['name'];
                        $menuArr2[$parent_menu_id]['icon'] = $menu['icon'];
                    }
                }
                if (!isset($menuArr2[$parent_menu_id]["child"])) {
                    $menuArr2[$parent_menu_id]["child"] = array();
                }
                array_push($menuArr2[$parent_menu_id]["child"], $v);
            }
        }

        //去掉数组的主键
        $menudata = array();
        foreach ($menuArr2 as $v) {
            array_push($menudata, $v);
        }
        if ($role_id == 1) {
            $menudata = AuthMenu::with(['child' => function ($q) {
                $q->where('status', 1);
                $q->orderBy('display_order', 'asc');
            }])->where(['parent_menu_id' => 0, 'status' => 1])->orderBy('display_order', 'asc')->get();
        }
        $array['adminName'] = Admin::where('id', $this->aId)->value('username');
        $array['menus'] = $menudata;
        return showMsg('获取成功', 200, $array);
    }

    public function userInfo(Request $request)
    {
        $com_id = $this->aId;
        $data1 = Admin::where('id', $com_id)->first();
        return showMsg('获取成功', 200, $data1);
    }

    //获取所有的角色
    public function gerRoleList()
    {
        $result = AuthRole::get();
        return showMsg('获取成功', 200, $result);
    }

}
